package ian.android.lovewords.util;

import ian.android.lovewords.time.TimeManip;
import ian.android.lovewords.time.TimeStruct;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.StringTokenizer;

import org.apache.http.util.EncodingUtils;

import android.content.Context;
import android.util.Log;

public class StoreProgress {

	private final String FILE_NAME_TODAY_NEW = "today_new.dat";
	private final String FILE_NAME_TODAY_REVIEW = "today_review.dat";
	private final String FILE_NAME_EVERYDAY = "everyday_num.dat";
	private final String FILE_NAME_STARTDATE = "start_date.dat";
	private final String FILE_NAME_SETTINGS = "store_settings.dat";
	private final String FILE_NAME_WEIBO = "store_weibo.dat";
	
	private Context context;
	
	public StoreProgress( Context context )
	{
		this.context = context;
	}
	
	public void recordEverydayNum( int everyday )
	{
		String s = "";
		s += Integer.toString(everyday);
		writeFileData(FILE_NAME_EVERYDAY, s);
	}
	
	public int getEverydayNum()
	{
		String tempString = readFileData(FILE_NAME_EVERYDAY);
		StringTokenizer st = new StringTokenizer( tempString);
		int j = Integer.valueOf( st.nextToken() );
		return j;
	}
	
	public int getListSum( String BookTitle )
	{
		if( BookTitle == "Red Bible" )
			return 50;
		else if ( BookTitle == "3000" )
			return 30;
		return 0;
	}
	
	public TimeStruct getStartDate()
	{
		String tempString = readFileData(FILE_NAME_STARTDATE);
		StringTokenizer st = new StringTokenizer( tempString);
		TimeStruct ts = new TimeStruct();
		ts.Year = Integer.valueOf( st.nextToken() );
		ts.Month = Integer.valueOf( st.nextToken() );
		ts.Day = Integer.valueOf( st.nextToken() );
		return ts;
	}
	
	public void setStartDate2Today()
	{
		TimeStruct today = TimeManip.GetTimeStructNow();
		String s = "";
		s = s + today.Year + " " + today.Month + " " + today.Day;
		writeFileData(FILE_NAME_STARTDATE, s);
	}
	
	public int getTodayNewListNum()
	{
		String s = readFileData(FILE_NAME_TODAY_NEW);
		StringTokenizer st = new StringTokenizer( s );
		TimeStruct today = TimeManip.GetTimeStructNow();
		String y = "", m = "", d = "";
		if(s!="")
		{
			y = st.nextToken();
			m = st.nextToken();
			d = st.nextToken();
		}
		if ( Integer.toString(today.Year) != y 
				|| Integer.toString(today.Month) != m 
				|| Integer.toString(today.Day) != d)
			return -1;
		return Integer.valueOf( st.nextToken() );
	}
	
	public int getTodayNewWordNum()
	{
		String s = readFileData(FILE_NAME_TODAY_NEW);
		StringTokenizer st = new StringTokenizer( s );
		TimeStruct today = TimeManip.GetTimeStructNow();
		String y = "", m = "", d = "";
		if(s!="")
		{
			y = st.nextToken();
			m = st.nextToken();
			d = st.nextToken();
		}
		if ( Integer.toString(today.Year) != y 
				|| Integer.toString(today.Month) != m 
				|| Integer.toString(today.Day) != d)
			return -1;
		st.nextToken();
		return Integer.valueOf( st.nextToken() );
	}
	
	public String[] readSettings(){
		String[] a = new String[3];
		int i=0;
		a[0] = "";
		a[1] = "";
		String s = readFileData(FILE_NAME_SETTINGS);
		Log.i("read", s);
		StringTokenizer st = new StringTokenizer( s );
		
		while(st.hasMoreTokens() && i<2){
			a[i++] += st.nextToken();
			Log.i("read", a[i-1]);
		}
		return a;
	}
	
	public void recordSettings(boolean state,String time){
		String content = "";
		if(state == true)
			content+= "true "+time;
		else
			content+= "false "+time;
		Log.i("write", Boolean.toString(state)+" "+time);
		writeFileData(FILE_NAME_SETTINGS,content);
	}
	
	public String readWeibo(){
		String a = readFileData(FILE_NAME_WEIBO);
		return a;
	}
	
	public void recordWeibo(boolean state){
		String content = "";
		if(state == true)
			content+= "true";
		else
			content+= "false ";
		writeFileData(FILE_NAME_WEIBO,content);
	}
	
	public void recordNewToday( int ListNum, int WordNum )
	{
		TimeStruct today = TimeManip.GetTimeStructNow();
		String s = "";
		s = s + today.Year + " " + today.Month + " " + today.Day;
		s = s + " " + ListNum + " " + WordNum;
		writeFileData(FILE_NAME_TODAY_NEW, s);
	}
	
	public void recordReviewToday( int ListNum, int WordNum )
	{
		TimeStruct today = TimeManip.GetTimeStructNow();
		String s = "";
		s = s + today.Year + " " + today.Month + " " + today.Day;
		s = s + " " + ListNum + " " + WordNum;
		writeFileData(FILE_NAME_TODAY_REVIEW, s);
	}
	
	public int getTodayReviewWordNum()
	{
		String s = readFileData(FILE_NAME_TODAY_REVIEW);
		StringTokenizer st = new StringTokenizer( s );
		TimeStruct today = TimeManip.GetTimeStructNow();
		String y = "", m = "", d = "";
		if(s!="")
		{
			y = st.nextToken();
			m = st.nextToken();
			d = st.nextToken();
		}
		if ( Integer.toString(today.Year) != y 
				|| Integer.toString(today.Month) != m 
				|| Integer.toString(today.Day) != d)
			return -1;
		st.nextToken();
		return Integer.valueOf( st.nextToken() );
	}
	
	public int getTodayReviewListNum()
	{
		String s = readFileData(FILE_NAME_TODAY_REVIEW);
		StringTokenizer st = new StringTokenizer( s );
		TimeStruct today = TimeManip.GetTimeStructNow();
		String y = "", m = "", d = "";
		if(s!="")
		{
			y = st.nextToken();
			m = st.nextToken();
			d = st.nextToken();
		}
		if ( Integer.toString(today.Year) != y 
				|| Integer.toString(today.Month) != m 
				|| Integer.toString(today.Day) != d)
			return -1;
		return Integer.valueOf( st.nextToken() );
	}
	
	private void writeFileDataAppend( String fileName, String message )
	{
		try{
	       FileOutputStream fout =  context.openFileOutput(fileName, Context.MODE_APPEND);
	       byte [] bytes = message.getBytes(); 
	       fout.write(bytes); 
	       fout.close(); 
		}
	    catch(Exception e){ 
	       e.printStackTrace(); 
	    } 
	}
	
	private void writeFileData( String fileName, String message )
	{ 
	     try{ 
	        FileOutputStream fout =  context.openFileOutput(fileName, Context.MODE_PRIVATE);
	        byte [] bytes = message.getBytes(); 
	        fout.write(bytes); 
	        fout.close(); 
	     } 
	     catch(Exception e){ 
	        e.printStackTrace(); 
	     }
	}
	
	private String readFileData(String fileName){ 
        String res=""; 
        try{ 
         FileInputStream fin = context.openFileInput(fileName); 
         int length = fin.available(); 
         byte [] buffer = new byte[length]; 
         fin.read(buffer);     
         res = EncodingUtils.getString(buffer, "UTF-8"); 
         fin.close();     
        } 
        catch(Exception e){ 
         e.printStackTrace(); 
        } 
        return res; 
    }
}
